<template>
  <div class="back">
    <div class="login">
      <h3>叩丁狼商品管理系统</h3>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            autocomplete="off"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon color="#454545" :size="24" class="no-inherit">
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            @keyup.enter="submitForm(ruleFormRef)"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon color="#454545" :size="24" class="no-inherit">
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import "@/assets/style.less";
import { ElMessage } from "element-plus";
import { loginAPI } from "@/view/login/api/index";
import { useRouter } from "vue-router";
const router = useRouter();

const ruleFormRef = ref<FormInstance>();

const checkAge = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("Please input the age"));
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error("Please input digits"));
    } else {
      if (value < 18) {
        callback(new Error("Age must be greater than 18"));
      } else {
        callback();
      }
    }
  }, 1000);
};

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入用户名"));
  } else {
    if (ruleForm.password !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  username: "",
  password: "",
  // age: '',
});

const rules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ validator: validatePass2, trigger: "blur" }],
  // age: [{ validator: checkAge, trigger: 'blur' }],
});

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      loginAPI(ruleForm).then((res) => {
        if (res.code === 200) {
          console.log(res);
          // 1.存token
          localStorage.setItem("token", res.data.tokenHead + res.data.token);
          // 2.跳转
          router.push("/");
          // 3.提示
          ElMessage.success("登录成功");
        }
      });
    } else {
      ElMessage.error("用户名或者密码不正确");
      return false;
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>
<style scoped lang="less">
.back {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  overflow: hidden;

  .login {
    margin: 300px auto;
    width: 400px;
    height: 200px;

    // border: 1px solid #000;
    h3 {
      padding: 20px 0;
      color: #fff;
      text-align: center;
    }

    :deep(.el-input__wrapper) {
      height: 50px;
    }

    :deep(.el-button--primary) {
      width: 100%;
      height: 50px;
    }
  }
}
</style>
